package com.cjk.console.tunnel;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TunnelMng {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(TunnelMng.class);

	public static String formatCommand(Integer localPort, String remoteHost, String remoteService) {
		try {
			FileReader privateKeyFileReader = new FileReader("/home/sshTunnel/.ssh/sshTunnel_rsa");
			FileReader publicKeyFileReader = new FileReader("/home/sshTunnel/.ssh/sshTunnel_rsa.pub");
			char[] keyBuf = new char[2048];

			int length = privateKeyFileReader.read(keyBuf);
			String privateKey = new String(keyBuf, 0, length);

			length = publicKeyFileReader.read(keyBuf);
			String publicKey = new String(keyBuf, 0, length);

			privateKeyFileReader.close();
			publicKeyFileReader.close();
			
			String command = TunnelConfig.getCommand();
			command = command.replace("[--[publicKey]--]", publicKey);
			command = command.replace("[--[privateKey]--]", privateKey);
			command = command.replace("[--[localPort]--]", String.valueOf(localPort));
			command = command.replace("[--[remoteHost]--]", remoteHost);
			command = command.replace("[--[remoteService]--]", remoteService);
			return command;
		} catch (FileNotFoundException e) {
			LOGGER.error(e.getMessage());
		} catch (IOException e) {
			LOGGER.error(e.getMessage());
		}
		return null;
	}
	
}
